<?php

namespace App\Http\Controllers\Admin;


use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use App\Repositories\User\UserApi;

class LoginController extends Controller {


	private $redirectAfterLogout = 'admin/enter';

	private $userApi;

	public function __construct(UserApi $userApi) {
		$this->middleware('guest:admin', ['except' => 'getLogout']);
		$this->userApi = $userApi;
	}

	public function getIndex() {
		return view('admin.login');
	}

	public function postLogin(Request $request) {
		if ($this->userApi->login($request)) {
			return redirect('admin');
		}
		return redirect('admin/enter')
			->withInput($request->only('authKey', 'remember'))
			->withErrors([
				'message' => $this->userApi->authError,
			]);
	}

	public function getLogout() {

		$this->userApi->logout();

		return redirect($this->redirectAfterLogout);
	}


}
